package com.qdlc.p2p.biz.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.log.score.BaseScoreLog;
import com.qdlc.p2p.biz.log.score.identify.CertificateScoreLog;
import com.qdlc.p2p.biz.service.UserCertificationService;
import com.qdlc.p2p.biz.service.VerifyLogService;
import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.dao.UserCertificationDao;
import com.qdlc.p2p.dal.dto.Operator;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.dto.UserCertification;
import com.qdlc.p2p.dal.dto.VerifyLog;
import com.qdlc.p2p.dal.model.UserCertificationModel;

@Service("userCertificationService")
public class UserCertificationServiceImpl implements UserCertificationService {

    @Resource
    private UserCertificationDao userCertificationDao;

    @Resource
    private VerifyLogService verifyLogService;

    @Override
    public PageDataList<UserCertificationModel> list(long userId, int page) {
        return userCertificationDao.list(userId, page);
    }

    @Override
    public void add(UserCertification certification) {
        userCertificationDao.save(certification);
    }

    /** 通过userId 获得资料审核列表 * */
    @Override
    public PageDataList<UserCertificationModel> findByUserId(long userId, int status, int page) {
        return userCertificationDao.findByUserId(userId, status, page);
    }

    @Override
    public void certificationVerify(long id, String verifyRemark, int status, Operator operator) {
        // 审核日志表中插入一条数据
        VerifyLog verifyLog = new VerifyLog();
        verifyLog.setVerifyUser(operator);
        verifyLog.setType("atteatation");
        verifyLog.setFid(id);
        verifyLog.setVerifyType(10);// 审核类型
        if (status == 1) {
            UserCertification attestation = userCertificationDao.find(id);
            BaseScoreLog bLog = new CertificateScoreLog(attestation.getUser().getUserId());
            bLog.doEvent();
            verifyLog.setResult(1);
        } else if (status == 2) {
            verifyLog.setResult(-1);
        }
        verifyLog.setRemark(verifyRemark);
        verifyLog.setTime(DateUtil.getNow());
        verifyLog.setIp(Global.getIP());
        verifyLogService.save(verifyLog);
        userCertificationDao.attestationEdit(id, verifyRemark, status, operator);
    }

    @Override
    public UserCertification findById(long id) {
        return userCertificationDao.find(id);
    }

    @Override
    public List<UserCertification> findByUserAndTypeId(User user, long typeId) {
        QueryParam param = QueryParam.getInstance()
                .addParam("certificationType.id", typeId)
                .addParam("user", user);
        return userCertificationDao.findByCriteria(param);
    }

    @Override
    public void delete(long id) {
        userCertificationDao.delete(id);
    }

    @Override
    public PageDataList<UserCertificationModel> userCertificationList(
            UserCertificationModel model) {
        return userCertificationDao.userCertificationList(model);
    }

    @Override
    public List<String> findByUserIdAndTypeId(long userId, long typeId) {
        return userCertificationDao.findByUserIdAndTypeId(userId, typeId);
    }
}
